﻿using System.Collections.Generic;
using StockholmsStad.Web.UI.Class.Interfaces;
using StockholmsStad.Web.UI.UIServiceReference;

namespace StockholmsStad.Web.UI.Class.Controller
{
    /// <summary>
    /// MVC Controller för bokningen
    /// </summary>
    public class OurRegistrationController : IRegistrationInformationController
    {
        private readonly IRegistration2_Contact _contactView;

        private readonly ILoginModel _loginModel;

        private readonly IRegistration1_Persons _personsView;

        /// <summary>
        /// Modellen som kontrollern jobbar mot
        /// </summary>
        private readonly IRegistrationModel _registrationModel;

        private readonly IRegistration3_Summary _summaryView;

        /// <summary>
        /// Skapar Controller och dess vyer och modellen
        /// </summary>
        /// <param name="loginModel"></param>
        /// <param name="summaryView"></param>
        /// <param name="personsView"></param>
        /// <param name="contactView"></param>
        /// <param name="model"></param>
        public OurRegistrationController(IRegistrationModel model, ILoginModel loginModel,
                                         IRegistration3_Summary summaryView, IRegistration1_Persons personsView,
                                         IRegistration2_Contact contactView)
        {
            _summaryView = summaryView;
            _personsView = personsView;
            _contactView = contactView;
            _registrationModel = model;
            _loginModel = loginModel;
        }

        /// <summary>
        /// Ska köras då sidan laddas
        /// </summary>
        public void PageLoad()
        {
            User user = _loginModel.User;
            _registrationModel.UpdateCoupleFromServer(user);
            _registrationModel.UpdateCeremonyFromServer(user);
            Couple couple = _registrationModel.Couple;
            Person person1 = couple.Person1;
            _summaryView.Person1 = person1;
            _summaryView.Person2 = couple.Person2;
            ContactPerson contactPerson = couple.ContactPerson;
            _summaryView.Contact = contactPerson ?? new ContactPerson
                                                        {
                                                            Email = person1.Email,
                                                            FirstNames = person1.FirstNames,
                                                            LastName = person1.LastName,
                                                            Telephone = person1.PhoneNumber,
                                                        };
            _summaryView.Ceremony = _registrationModel.Ceremony;
        }

        ///<summary>
        /// Körs då användaren bestämmer sig för att uppdatera registreringsinformationen
        ///</summary>
        public void UpdateChangablePages()
        {
            Couple couple = _registrationModel.Couple;
            Person person1 = couple.Person1;
            _personsView.UpdatePersons(person1, couple.Person2);
            bool contactIsFirstPerson;
            ContactPerson contactPerson;
            if (couple.ContactPerson != null)
            {
                contactPerson = couple.ContactPerson;
                contactIsFirstPerson = false;
            }
            else
            {
                contactPerson = new ContactPerson
                                    {
                                        Email = person1.Email,
                                        FirstNames = person1.FirstNames,
                                        LastName = person1.LastName,
                                        Telephone = person1.PhoneNumber,
                                    };
                contactIsFirstPerson = true;
            }
            _contactView.UpdateContactAndCeremony(contactPerson, _registrationModel.Ceremony, contactIsFirstPerson);
            _contactView.UpdateContactPersonListName(person1.FirstNames + " " + person1.LastName);
        }

        /// <summary>
        /// Sparar person1 och person2 temporärt
        /// </summary>
        /// <param name="person1"></param>
        /// <param name="person2"></param>
        public void RegisterPersons(Person person1, Person person2)
        {
            Couple couple = _registrationModel.Couple;
            couple.Person1 = person1;
            couple.Person2 = person2;
            _contactView.UpdateContactPersonListName(_registrationModel.FullNameFromPerson1);
            UpdateContactName(true);
        }

        /// <summary>
        /// Sparar kontaktperson och ceremoniinformation temporärt
        /// </summary>
        /// <param name="contact"></param>
        /// <param name="information"></param>
        /// <param name="contactPersonIsFirstPerson"></param>
        public void RegisterContact(ContactPerson contact, Ceremony information, bool contactPersonIsFirstPerson)
        {
            Couple couple = _registrationModel.Couple;
            if (contactPersonIsFirstPerson)
            {
                couple.Person1.Email = contact.Email;
                couple.Person1.PhoneNumber = contact.Telephone;
                couple.ContactPerson = null;
            }
            else
            {
                couple.Person1.Email = null;
                couple.Person1.PhoneNumber = null;
                couple.ContactPerson = contact;
            }
            _registrationModel.Ceremony = information;
        }

        /// <summary>
        /// Bekräftar att person1, person2, kontaktperson och ceremoniinforamtion ska sparas
        /// på server.
        /// </summary>
        public void SubmitRegistrationUpdate(ContactPerson contact)
        {
            _registrationModel.RegisterCouple(_loginModel.User);
            //Kolla om email har ändrats och om vi måste uppdatera inloggad användare
            if(!_loginModel.User.Username.Equals(contact.Email))
            {
                _loginModel.User.Username = contact.Email;
            }
        }

        #region Implementation of IRegistrationInformationController

        /// <summary>
        /// Uppdaterar kontaktpersonsfältet beroende på om användaren väljer
        /// person1 eller annan person som kontaktperson
        /// </summary>
        public void UpdateContactName(bool onlyPerson1)
        {
            if (_contactView.ContactPersonListIsFirstPerson())
            {
                Person person1 = _registrationModel.Couple.Person1;
                _contactView.UpdateContactPersonName(person1.FirstNames, person1.LastName);
                _contactView.UpdateContactPersonName(false);
            }
            else if(!onlyPerson1)
            {
                _contactView.UpdateContactPersonName("", "");
                _contactView.UpdateContactPersonDetails("", "");
                _contactView.UpdateContactPersonName(true);
            }
        }

        /// <summary>
        /// Alla tillgängliga ceremonispråk
        /// </summary>
        public IEnumerable<ValueIdPair> GetCeremonyLanguages()
        {
            return _registrationModel.CeremonyLanguages;
        }

        /// <summary>
        /// Alla tillgängliga specialbehov
        /// </summary>
        public IEnumerable<ValueIdPair> GetCeremonySpecialNeeds()
        {
            return _registrationModel.CeremonySpecialNeeds;
        }

        /// <summary>
        /// Alla tillgängliga vigselbevisspråk
        /// </summary>
        public IEnumerable<ValueIdPair> GetWeddingCertificateLanguages()
        {
            return _registrationModel.WeddingCertificateLanguages;
        }

        ///<summary>
        /// Tar reda på om användarnamnet är unikt
        ///</summary>
        ///<param name="username"></param>
        ///<returns></returns>
        public bool IsUserNameUnique(string username)
        {
            User user = _loginModel.User;
            return _registrationModel.IsUserNameUnique(user, username);
        }

        #endregion
    }
}